package cn.fintecher.pangolin.service.business.service;

import cn.fintecher.pangolin.common.enums.CasePool;
import cn.fintecher.pangolin.common.model.UserModel;
import cn.fintecher.pangolin.entity.elastic.BaseCase;
import cn.fintecher.pangolin.entity.elastic.HistoryCases;
import cn.fintecher.pangolin.entity.elastic.PaymentRecord;
import cn.fintecher.pangolin.service.business.model.request.DebtMessageRequest;
import cn.fintecher.pangolin.service.business.model.response.PaymentRecordResponse;
import cn.fintecher.pangolin.service.business.repository.elasticsearch.BaseCaseRespository;
import cn.fintecher.pangolin.service.business.repository.elasticsearch.HistoryCasesRespository;
import cn.fintecher.pangolin.service.business.repository.elasticsearch.PaymentRecordElastic;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.IterableUtils;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.modelmapper.ModelMapper;
import org.modelmapper.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Type;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Auther: xiaqun
 * @Description:
 * @Date: 14:51 2019/10/11
 */

@Service("paymentRecordService")
public class PaymentRecordService {
    @Autowired
    private PaymentRecordElastic paymentRecordElastic;

    @Autowired
    private ModelMapper modelMapper;

    @Autowired
    private BaseCaseRespository baseCaseRespository;

    @Autowired
    private HistoryCasesRespository historyCasesRespository;

    @Autowired
    private BaseService baseService;

    /**
     * 查询还款记录
     */
    public List<PaymentRecordResponse> findPaymentRecord(String caseId) {
        BoolQueryBuilder builder = new BoolQueryBuilder();
        builder.must(QueryBuilders.matchPhraseQuery("caseId.keyword", caseId));
        builder.must(QueryBuilders.matchPhraseQuery("flag", 0));
        Iterable<PaymentRecord> iterable = paymentRecordElastic.search(builder);
        List<PaymentRecord> list = IterableUtils.toList(iterable);
        if (!list.isEmpty()) {
            list.sort((e1, e2) -> e2.getPaymentDate().compareTo(e1.getPaymentDate()));
        }
        Type listType = new TypeToken<List<PaymentRecordResponse>>() {
        }.getType();
        return modelMapper.map(list, listType);
    }

    /**
     * @Author wangxiangdong
     * @Description 共债还款记录查询
     * @Date 2019/10/28 16:03
     **/
    public List<PaymentRecordResponse> caseBillRecord(DebtMessageRequest debtMessageRequest, String token) {
        UserModel userModel = baseService.getUserByToken(token);
        List<String> ids;
        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
        baseService.spliceBuilder(queryBuilder, userModel);
        queryBuilder.must(QueryBuilders.termsQuery("certificateNo.keyword", debtMessageRequest.getCertificateNo()));
        if (StringUtils.isNotBlank(debtMessageRequest.getBatchNumber())) {
            queryBuilder.must(QueryBuilders.termsQuery("batchNumber.keyword", debtMessageRequest.getBatchNumber()));
        }
        if (Objects.equals(CasePool.INPOOLCASE, debtMessageRequest.getType())) {
            List<BaseCase> baseCaseList = Lists.newArrayList(baseCaseRespository.search(queryBuilder));
            ids = baseCaseList.stream().map(BaseCase::getId).collect(Collectors.toList());
        } else {
            List<HistoryCases> historyCasesList = Lists.newArrayList(historyCasesRespository.search(queryBuilder));
            ids = historyCasesList.stream().map(HistoryCases::getId).collect(Collectors.toList());
        }
        ids.removeIf(x->Objects.equals(x,debtMessageRequest.getCaseId()));
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        boolQueryBuilder.must(QueryBuilders.termsQuery("caseId.keyword", ids));
        boolQueryBuilder.must(QueryBuilders.matchPhraseQuery("flag", 0));
        List<PaymentRecord> paymentRecordList = Lists.newArrayList(paymentRecordElastic.search(boolQueryBuilder));
        Type typeToken = new TypeToken<List<PaymentRecordResponse>>() {
        }.getType();
        return modelMapper.map(paymentRecordList, typeToken);
    }
}
